/*--------------------------------------------------------------------------------------*\
**
** adv_ferry.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adventure_events_visiblity.h"

#include "adventure_map.h"
#include "adventure_map_window.h"
#include "adventure_object.h"
#include "adventure_object_ptr.h"
#include "army.h"
#include "compressed_filter.h"
#include "game_time.h"
#include "saved_game_header.h"
#include "shroud_removers.h"

namespace 
{
	const int k_memory_event_buffer_size	= 0;
	const int k_event_lifespan				= 0*1000;			//in mili-seconds
};

t_adventure_event_visiblity::t_adventure_event_visiblity ( ) 
: t_adventure_event_base ( k_visiblity_event ) 
{ 	
}

t_adventure_event_visiblity::t_adventure_event_visiblity ( t_army* army, t_adv_map_point const& point,
														   t_direction direction, t_adventure_object* source ) 
: t_adventure_event_base ( k_visiblity_event ) 
{ 
	m_global_id		= army->get_global_id();
	
	m_direction			= direction;
	m_event_point		= point;
	m_global_id_source	= source->get_global_id();
}

void t_adventure_event_visiblity::execute_event( t_adventure_map* map, t_saved_game_header const & header ) 
{	
	t_adventure_object * army_object	= map->get_adventure_object_with_gid ( m_global_id );
	t_adventure_object * source_object	= map->get_adventure_object_with_gid ( m_global_id_source );
		
	if (army_object == NULL || source_object == NULL) 
		return;

	t_army * army = dynamic_cast<t_army*>(army_object);
	t_shroud_remover * shroud_remover = dynamic_cast<t_shroud_remover*>(source_object);

	if (army == NULL || shroud_remover == NULL) 
		return;

	shroud_remover->do_logic( army, m_event_point, m_direction, army->get_map()->get_map_window()->get_frame());

	m_time_end = get_time() + k_event_lifespan;

	return;
}

void t_adventure_event_visiblity::undo_event( t_adventure_map* map , t_saved_game_header const & header )
{
	return;
}

bool t_adventure_event_visiblity::read( std::streambuf & buffer , int version )
{
	m_direction	= get<t_direction>(buffer);
	buffer >> m_event_point;
	m_global_id_source	= get<t_adventure_global_id>(buffer);
	
	return t_adventure_event_base::read( buffer , version );
}

bool t_adventure_event_visiblity::write( std::streambuf & buffer )
{
	put<t_direction>(buffer,m_direction);
	buffer << m_event_point;
	put<t_adventure_global_id>(buffer,m_global_id_source);

	return t_adventure_event_base::write( buffer );
}
